<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="google" value="notranslate">
  <title>Blockly</title>
  <link rel="stylesheet" href="style.css">
  <script src="./storage.js"></script>
  <script src="./blockly_compressed.js"></script>
  <script src="./blocks_compressed.js"></script>
  <script src="./javascript_compressed.js"></script>
  <script src="./python_compressed.js"></script>
  <script src="./php_compressed.js"></script>
  <script src="./lua_compressed.js"></script>
  <script src="./dart_compressed.js"></script>
  <script src="./code.js"></script>

</head>

<body>
  <table width="100%" height="100%">
    <tr>
      <td colspan=2>
        <table width="100%">
          <tr id="tabRow" height="1em">
            <td id="tab_blocks" class="tabon">Blocks</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_javascript" class="taboff tab_collapse">JavaScript</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_python" class="taboff tab_collapse">Python</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_php" class="taboff tab_collapse">PHP</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_lua" class="taboff tab_collapse">Lua</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_dart" class="taboff tab_collapse">Dart</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_xml" class="taboff tab_collapse">XML</td>
            <td class="tabmin tab_collapse">&nbsp;</td>
            <td id="tab_json" class="taboff tab_collapse">JSON</td>
            <td class="tabmin">&nbsp;</td>
            <td id="tab_code" class="taboff tab_collapse">
              <select id="code_menu"></select>
            </td>
            <td class="tabmax">
              <button onclick="window.location.href='/account/center'">返回</button>
              <select class="farSide" id="languageMenu"></select>
              <input type="text" id="project-name" placeholder="My blocks"></input>
              <button id="submitButton" class="notext" title="发布">
                <span style="font-size:18px;">发布</span>
                <img src='./media/1x1.gif' class="upload icon21">
              </button>
              <button id="trashButton" class="notext" title="...">
                <img src='./media/1x1.gif' class="trash icon21">
              </button>
              <!-- <button id="linkButton" class="notext" title="...">
                <img src='./media/1x1.gif' class="link icon21">
              </button> -->
              <button id="runButton" class="notext primary" title="...">
                <img src='./media/1x1.gif' class="run icon21">
              </button>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td height="99%" colspan=2 id="content_area">
      </td>
    </tr>
  </table>
  <div id="content_blocks" class="content"></div>
  <pre id="content_javascript" class="content prettyprint lang-js"></pre>
  <pre id="content_python" class="content prettyprint lang-py"></pre>
  <pre id="content_php" class="content prettyprint lang-php"></pre>
  <pre id="content_lua" class="content prettyprint lang-lua"></pre>
  <pre id="content_dart" class="content prettyprint lang-dart"></pre>
  <textarea id="content_xml" class="content" wrap="off"></textarea>
  <textarea id="content_json" class="content" wrap="off"></textarea>

  <xml xmlns="https://developers.google.com/blockly/xml" id="toolbox" style="display: none">
    <category name="%{BKY_CATLOGIC}" colour="%{BKY_LOGIC_HUE}">
      <block type="controls_if"></block>
      <block type="logic_compare"></block>
      <block type="logic_operation"></block>
      <block type="logic_negate"></block>
      <block type="logic_boolean"></block>
      <block type="logic_null"></block>
      <block type="logic_ternary"></block>
    </category>
    <category name="%{BKY_CATLOOPS}" colour="%{BKY_LOOPS_HUE}">
      <block type="controls_repeat_ext">
        <value name="TIMES">
          <shadow type="math_number">
            <field name="NUM">10</field>
          </shadow>
        </value>
      </block>
      <block type="controls_whileUntil"></block>
      <block type="controls_for">
        <value name="FROM">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <value name="TO">
          <shadow type="math_number">
            <field name="NUM">10</field>
          </shadow>
        </value>
        <value name="BY">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
      </block>
      <block type="controls_forEach"></block>
      <block type="controls_flow_statements"></block>
    </category>
    <category name="%{BKY_CATMATH}" colour="%{BKY_MATH_HUE}">
      <block type="math_number">
        <field name="NUM">123</field>
      </block>
      <block type="math_arithmetic">
        <value name="A">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <value name="B">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
      </block>
      <block type="math_single">
        <value name="NUM">
          <shadow type="math_number">
            <field name="NUM">9</field>
          </shadow>
        </value>
      </block>
      <block type="math_trig">
        <value name="NUM">
          <shadow type="math_number">
            <field name="NUM">45</field>
          </shadow>
        </value>
      </block>
      <block type="math_constant"></block>
      <block type="math_number_property">
        <value name="NUMBER_TO_CHECK">
          <shadow type="math_number">
            <field name="NUM">0</field>
          </shadow>
        </value>
      </block>
      <block type="math_round">
        <value name="NUM">
          <shadow type="math_number">
            <field name="NUM">3.1</field>
          </shadow>
        </value>
      </block>
      <block type="math_on_list"></block>
      <block type="math_modulo">
        <value name="DIVIDEND">
          <shadow type="math_number">
            <field name="NUM">64</field>
          </shadow>
        </value>
        <value name="DIVISOR">
          <shadow type="math_number">
            <field name="NUM">10</field>
          </shadow>
        </value>
      </block>
      <block type="math_constrain">
        <value name="VALUE">
          <shadow type="math_number">
            <field name="NUM">50</field>
          </shadow>
        </value>
        <value name="LOW">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <value name="HIGH">
          <shadow type="math_number">
            <field name="NUM">100</field>
          </shadow>
        </value>
      </block>
      <block type="math_random_int">
        <value name="FROM">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <value name="TO">
          <shadow type="math_number">
            <field name="NUM">100</field>
          </shadow>
        </value>
      </block>
      <block type="math_random_float"></block>
      <block type="math_atan2">
        <value name="X">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <value name="Y">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
      </block>
    </category>
    <category name="%{BKY_CATTEXT}" colour="%{BKY_TEXTS_HUE}">
      <block type="text"></block>
      <block type="text_join"></block>
      <block type="text_append">
        <value name="TEXT">
          <shadow type="text"></shadow>
        </value>
      </block>
      <block type="text_length">
        <value name="VALUE">
          <shadow type="text">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
      <block type="text_isEmpty">
        <value name="VALUE">
          <shadow type="text">
            <field name="TEXT"></field>
          </shadow>
        </value>
      </block>
      <block type="text_indexOf">
        <value name="VALUE">
          <block type="variables_get">
            <field name="VAR">{textVariable}</field>
          </block>
        </value>
        <value name="FIND">
          <shadow type="text">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
      <block type="text_charAt">
        <value name="VALUE">
          <block type="variables_get">
            <field name="VAR">{textVariable}</field>
          </block>
        </value>
      </block>
      <block type="text_getSubstring">
        <value name="STRING">
          <block type="variables_get">
            <field name="VAR">{textVariable}</field>
          </block>
        </value>
      </block>
      <block type="text_changeCase">
        <value name="TEXT">
          <shadow type="text">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
      <block type="text_trim">
        <value name="TEXT">
          <shadow type="text">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
      <block type="text_print">
        <value name="TEXT">
          <shadow type="text">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
      <block type="text_prompt_ext">
        <value name="TEXT">
          <shadow type="text">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
    </category>
    <category name="%{BKY_CATLISTS}" colour="%{BKY_LISTS_HUE}">
      <block type="lists_create_with">
        <mutation items="0"></mutation>
      </block>
      <block type="lists_create_with"></block>
      <block type="lists_repeat">
        <value name="NUM">
          <shadow type="math_number">
            <field name="NUM">5</field>
          </shadow>
        </value>
      </block>
      <block type="lists_length"></block>
      <block type="lists_isEmpty"></block>
      <block type="lists_indexOf">
        <value name="VALUE">
          <block type="variables_get">
            <field name="VAR">{listVariable}</field>
          </block>
        </value>
      </block>
      <block type="lists_getIndex">
        <value name="VALUE">
          <block type="variables_get">
            <field name="VAR">{listVariable}</field>
          </block>
        </value>
      </block>
      <block type="lists_setIndex">
        <value name="LIST">
          <block type="variables_get">
            <field name="VAR">{listVariable}</field>
          </block>
        </value>
      </block>
      <block type="lists_getSublist">
        <value name="LIST">
          <block type="variables_get">
            <field name="VAR">{listVariable}</field>
          </block>
        </value>
      </block>
      <block type="lists_split">
        <value name="DELIM">
          <shadow type="text">
            <field name="TEXT">,</field>
          </shadow>
        </value>
      </block>
      <block type="lists_sort"></block>
    </category>
    <category name="%{BKY_CATCOLOUR}" colour="%{BKY_COLOUR_HUE}">
      <block type="colour_picker"></block>
      <block type="colour_random"></block>
      <block type="colour_rgb">
        <value name="RED">
          <shadow type="math_number">
            <field name="NUM">100</field>
          </shadow>
        </value>
        <value name="GREEN">
          <shadow type="math_number">
            <field name="NUM">50</field>
          </shadow>
        </value>
        <value name="BLUE">
          <shadow type="math_number">
            <field name="NUM">0</field>
          </shadow>
        </value>
      </block>
      <block type="colour_blend">
        <value name="COLOUR1">
          <shadow type="colour_picker">
            <field name="COLOUR">#ff0000</field>
          </shadow>
        </value>
        <value name="COLOUR2">
          <shadow type="colour_picker">
            <field name="COLOUR">#3333ff</field>
          </shadow>
        </value>
        <value name="RATIO">
          <shadow type="math_number">
            <field name="NUM">0.5</field>
          </shadow>
        </value>
      </block>
    </category>
    <sep></sep>
    <category name="%{BKY_CATVARIABLES}" colour="%{BKY_VARIABLES_HUE}" custom="VARIABLE"></category>
    <category name="%{BKY_CATFUNCTIONS}" colour="%{BKY_PROCEDURES_HUE}" custom="PROCEDURE"></category>
  </xml>

</body>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cokie.min.js"></script>
<script src="../js/jquery.qrcode.min.js"></script>
<script src="../js/qiniu.min.js"></script>
<script src="../js/common.js?v=27"></script>
<script>
  var uploadParam = {}
  var workName = ''
  var workId = urlParams('workId')
  var unitId = urlParams('unitId')
  var departId = urlParams('departId') //班级ID
  var additionalId = urlParams('additionalId') //附加作业ID
  var scene = urlParams("scene") || 'create' //来源
  var access_token = getUserToken()
  var userInfo = getUserInfo();
  var qn_token = getQiniuToken();
  setInterval(function () {
    qn_token = getQiniuToken();
  }, 600 * 1000)

  //点击提交按钮
  $("#submitButton").click(() => {
    qn_token = getQiniuToken((res)=>{}, (e)=>{
      console.log(e);
      if(e.responseJSON.message == 'Token失效，请重新登录'){
        alert('请登录后提交')
      }
    });
    let xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace());
    let text = Blockly.Xml.domToText(xml);
    console.log(text);
    let blob = new Blob([text], { type: 'text/plain' })
    upload2Qiniu(blob, 'blockly/' + window.uuid() + '.xml', $("#project-name").val(), observer)
  })

  //加载blob项目
  function loadBlobProject(blob, name) {
    console.log(blob);
    let reader = new FileReader();
    reader.onload = function (e) {
      let text = e.target.result
      let xml = Blockly.Xml.textToDom(text)
      console.log(xml);
      let workspace = Blockly.getMainWorkspace();
      workspace.clear();
      if (xml) {
        Blockly.Xml.domToWorkspace(xml, workspace);
        $("#project-name").val(name)
      }
    }
    reader.readAsText(blob);
  }

  //加载xml文本项目
  function loadProject(xmlText, name) {
    let xml = Blockly.Xml.textToDom(xmlText)
    console.log(xml);
    let workspace = Blockly.getMainWorkspace();
    workspace.clear();
    if (xml) {
      Blockly.Xml.domToWorkspace(xml, workspace);
      $("#project-name").val(name)
    }
  }

  var observer = {
    next(res) { },
    error(err) {
      console.log(1, err)
    },
    complete(res) {
      uploadParam.projectKey = uploadFile(res.key, '学生作业-blockly', res.key, 2)
      uploadParam.projectTitle = $("#project-name").val() ? $("#project-name").val() : "Blockly code"
      uploadWork(uploadParam.projectKey, uploadParam.projectTitle)
    }
  }

  //加载作品
  if (workId) {
    getWorkInfo(workId, function (info) {
      $.ajax({
        url: info.workFileKey_url,
        type: 'GET',
        success: function (res) {
          loadProject(res, info.workName)
        },
      })
    })
  }

  //上传作业
  function uploadWork(projectKey, projectTitle) {
    $.ajax({
      url: '/api/teaching/teachingWork/submit',
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json',
      beforeSend: function (request) {
        request.setRequestHeader('X-Access-Token', access_token)
      },
      data: JSON.stringify({
        courseId: unitId,
        workCover: "",
        workFile: projectKey,
        workName: projectTitle,
        id: workId,
        workType: 10,
        workStatus: 1,
        departId: departId,
        additionalId: additionalId,
        workScene: scene,
      }),
      success: function (res) {
        if (res.code == 200) {
          alert("提交成功")
          // createCode('qrcode', shareUrl)
        } else {
        }
      },
      error: function () {
      },
      complete: function () {
      }
    })
  }
</script>

</html>